import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/core/values/images.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/demand_research/get_personal_result_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/education/quiz/model.dart';
import 'package:enterprise_v6_app/app/modules/application/education/quiz/result/model.dart';
import 'package:get/get.dart';

///调研结果 （调研测试）
class FindingsTextController extends GetxController {
  List<String> list = [];
  String? recommendation = '';
  QuizResultPageData? data;

  /// 所有的题目的配置 (Get.arguments as QuizConfig?) ??
  var quizConfig = QuizConfig(
    type: QuizType.demandTest,
    topics: <QuizTopic>[
    ],
  );

  @override
  void onInit() {
    super.onInit();
    //获取我的调研结果
    getPersonalResult();
  }

  ///获取我的调研结果
  void getPersonalResult() async {
    final res = await HttpUtil().generateGetRequest<GetPersonalResultEntity>(Interface.getPersonalResult, queryParameters: {
      'surveyId': Get.arguments['id'] ?? '',
    }).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        //获取建议
        recommendation = result.data?.recommendation ?? '';
        startDoingQuestions(result.data?.selectPositionList);
        //获取已选专题
        if (result.data?.selectTypeList != null) {
          final List<GetPersonalResultSelectTypeList> questionValidityName = result.data?.selectTypeList ?? [];
          for (var value in questionValidityName) {
            if (value == questionValidityName.last) {
              list.add(value.dictionaryLabel ?? '');
            } else {
              list.add('${value.dictionaryLabel}、');
            }
          }
        }
      }
    });
    update();
  }

  ///题
  Future<void> startDoingQuestions(List<GetPersonalResultSelectPositionList>? listSurveyQuestion) async {
    final List<QuizTopic> topicsX = [];
    for (var listEntity in listSurveyQuestion!) {
      List<int>? answersX = [];
      final List<String> optionsX = [];
      if (listEntity.optionA != null) {
        optionsX.add('${listEntity.optionA}');
      }
      if (listEntity.optionB != null) {
        optionsX.add('${listEntity.optionB}');
      }
      if (listEntity.optionC != null) {
        optionsX.add('${listEntity.optionC}');
      }
      if (listEntity.optionD != null) {
        optionsX.add('${listEntity.optionD}');
      }
      //转码-对应的65 得到0的角标 A(65)-65=0
      answersX = listEntity.questionValidity?.split(';').map((e) => e.codeUnitAt(0) - 65).toList();
      final QuizTopic quizTopic= QuizTopic(
        id: listEntity.id ?? '',
        question: '${listEntity.questionMain}',
        answers: answersX,
        options: optionsX,
        score: listEntity.score ?? 0,
      );
      quizTopic.selectedIdx= listEntity.myValidity!.split(';').map((e) => e.codeUnitAt(0) - 65).toList();
      topicsX.add(quizTopic);
    }
    quizConfig = QuizConfig(
      type: QuizType.demandTest,
      topics: topicsX,
    );
    final correctCount = topicsX.fold(0, (pre, e) => pre + (e.isAnswerRight ? 1 : 0));
    final notAnsweredCount = topicsX.fold(0, (pre, e) => pre + (e.isAnswered ? 0 : 1));
    final notAnswerCount = notAnsweredCount;
    final answeredCount = topicsX.length - notAnswerCount;
    final wrongCount = topicsX.length - correctCount;
    data = QuizResultPageData(
      summary: {
        '答题数': answeredCount.toString(),
        '答对数': correctCount.toString(),
        '答错数': wrongCount.toString(),
        '未答数': notAnswerCount.toString(),
      },
      image: AppImages.icon_red_cal,
      title: '本次错题（$wrongCount道）',
      subtitle: '趁热打铁，巩固本次错题',
    );
  }
}
